<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "UserEducation".
 */
class UserEducation extends ActiveRecord {

    // @name 学历学位
    const DegreePrimary = 0;
    const DegreeMiddle = 1;
    const DegreeHigh = 2;
    const DegreeJunior = 3;
    const DegreeUniversity = 4;
    const DegreeGraduate = 5;
    const DegreeDoctor = 6;
    public static $degreeSelector = [
        self::DegreePrimary => ['title' => '小学', 'status' => 'gray'],
        self::DegreeMiddle => ['title' => '中学', 'status' => 'gray'],
        self::DegreeHigh => ['title' => '高中', 'status' => 'orange'],
        self::DegreeJunior => ['title' => '大专', 'status' => 'blue'],
        self::DegreeUniversity => ['title' => '大学', 'status' => 'blue'],
        self::DegreeGraduate => ['title' => '研究生', 'status' => 'red'],
        self::DegreeDoctor => ['title' => '博士', 'status' => 'red'],
    ];

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['user_id', 'school', 'begin', 'finish', 'major', 'degree'], 'required'],
            [['user_id', 'degree'], 'integer'],
            [['school', 'major'], 'string', 'max' => 32],
            [['begin', 'finish'], 'string', 'max' => 16],
        ];
    }
    /**
     * @name 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'user_id' => '用户名',
            'school' => '学校',
            'begin' => '入学时间',
            'finish' => '毕业时间',
            'degree' => '学历学位',
            'major' => '专业',
        ];
    }
    /**
     * @name update & insert data check config for html
     * @param $type string 页面操作类型
     * @param $encodeJson boolean 是否转成json串
     * @return string / array
     */
    public static function flyer($type = 'update')
    {
        // jsut search
        $rule = [
            'param' => [
                'school' => ['学校名称', ['maxlength' => 32, 'required']],
                'begin' => ['入学时间', ['date' => 'Y-m-d', 'required']],
                'finish' => ['毕业时间', ['date' => 'Y-m-d', 'required']],
                'degree' => ['学历学位', ['inkey' => static::$degreeSelector, 'required']],
                'major' => ['专业', ['maxlength' => 32, 'required']],
            ],
        ];
        return $rule;
    }

    /**
     * @name 获取user信息
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }
}